/*
 * Filename:    SelfFeedbackDetailServiceImpl.java
 * Copyright:   2014 中国平安保险（集团）股份有限公司 版权所有
 * Company:     平安科技养老险及健康险系统开发部
 * Create at:   2014年11月14日
 */
package com.paic.mhis.hcpms.warning.biz.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.paic.mhis.hcpms.common.document.dto.DocContentDTO;
import com.paic.mhis.hcpms.common.document.dto.DocParameterCondition;
import com.paic.mhis.hcpms.warning.biz.service.SelfFeedbackDetailService;
import com.paic.mhis.hcpms.warning.dao.SelfFeedbackDetailDAO;
import com.paic.mhis.hcpms.warning.dto.HcpmsWarnsDTO;
import com.paic.mhis.hcpms.warning.dto.WarningCheckDTO;
import com.paic.mhis.hcpms.warning.dto.WarningCheckDetailDTO;
import com.paic.mhis.hcpms.warning.intergration.dao.HcpmsWarnDAO;

@Service("selfFeedbackDetailService")
public class SelfFeedbackDetailServiceImpl implements SelfFeedbackDetailService {
	@Autowired
	private SelfFeedbackDetailDAO selfFeedbackDetailDao;

	@Autowired
	private HcpmsWarnDAO hcpmsWarnDao;

	@Override
	public List<WarningCheckDetailDTO> getFeedBackDetailList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return selfFeedbackDetailDao.getFeedBackDetailList(map);
	}

	@Override
	public List<WarningCheckDTO> getFeedBackList(Map<String, Object> map) {
		// TODO Auto-generated method stub
		return selfFeedbackDetailDao.getFeedBackList(map);
	}

	@Override
	public List<DocContentDTO> queryByDocParameterCondition(DocParameterCondition parameter) {
		// TODO Auto-generated method stub
		return selfFeedbackDetailDao.queryByDocParameterCondition(parameter);
	}

	// @Transactional
	@Override
	public int saveSelfFeedBack(JSONObject parameterJson) {
		// TODO Auto-generated method stub
		String warnNo = parameterJson.get("warnNo").toString();
		String corrective = parameterJson.get("check").toString();// 自查情况
		String resultsStatistical = parameterJson.get("corrective").toString();// 整改措施
		BigDecimal feeAmountCount = new BigDecimal(parameterJson.get("feeAmountCount").toString());

		WarningCheckDTO warnCheck = new WarningCheckDTO();
		warnCheck.setCheckMeasure(resultsStatistical);
		warnCheck.setWarnNo(warnNo);
		warnCheck.setCheckSumamount(feeAmountCount);
		warnCheck.setCheckSituation(corrective);
		Map<String, String> map = new HashMap<String, String>();
		map.put("warnNo", warnNo);
		JSONArray jsonArr = (JSONArray) parameterJson.remove("resultsStatistical");
		List<WarningCheckDetailDTO> list = new ArrayList<WarningCheckDetailDTO>();
		int result = 0;
		if (jsonArr.size() > 0) {
			for (int j = 0; j < jsonArr.size(); j++) {
				WarningCheckDetailDTO dto = new WarningCheckDetailDTO();
				JSONObject tempJson = jsonArr.getJSONObject(j);
				dto = JSONObject.toJavaObject(tempJson, WarningCheckDetailDTO.class);
				list.add(dto);
			}
		}
		try {
			selfFeedbackDetailDao.saveFeedBackList(warnCheck);
			if (updateFeedBackDetailList(map) > 0) {
				selfFeedbackDetailDao.saveFeedBackDetailList(list);
			}
			result = 1;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	public int updateFeedBackDetailList(Map map) {
		int result = 0;
		selfFeedbackDetailDao.updateFeedBackDetailList(map);
		result = 1;
		return result;
	}

	@Override
	public HcpmsWarnsDTO getWarnInfo(String warnNo) {
		// TODO Auto-generated method stub
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("warnNo", warnNo);
		HcpmsWarnsDTO dto = hcpmsWarnDao.getWarningInfo(map);
		return dto;
	}
}
